﻿using SQLRunnerLib.Project;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SQLRunnerLib.Exceptions;
namespace SQLUnitTests
{
    
    
    /// <summary>
    ///This is a test class for SQLEnvironmentTest and is intended
    ///to contain all SQLEnvironmentTest Unit Tests
    ///</summary>
    [TestClass()]
    public class SQLEnvironmentTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for Validate
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(ValidationException), "Environments must have a name.")]
        public void ValidateBlankEnvironmentTest()
        {
            SQLEnvironment environment = new SQLEnvironment();

            environment.Validate();
        }

        /// <summary>
        ///A test for Validate
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(ValidationException), "A server name is required.")]
        public void ValidateEnvironmentWithNoServerNameTest()
        {
            SQLEnvironment environment = new SQLEnvironment();
            environment.Name = "Test";
            environment.Validate();
        }

        /// <summary>
        ///A test for Validate
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(ValidationException), "A database name is required.")]
        public void ValidateEnvironmentWithNoDatabaseNameTest()
        {
            SQLEnvironment environment = new SQLEnvironment();
            environment.Name = "Test";
            environment.ServerName = "Server Name";
            environment.Validate();

            Assert.IsTrue(environment.HasFullCredentials);
        }

        /// <summary>
        ///A test for Validate
        ///</summary>
        ///<remarks>
        ///This test shows that the default for trusted is: true
        ///</remarks>
        [TestMethod()]
        public void ValidateEnvironmentWhenTrustedNotSetTest()
        {
            SQLEnvironment environment = new SQLEnvironment();
            environment.Name = "Test";
            environment.ServerName = "Server Name";
            environment.DatabaseName = "DBName";
            environment.Validate();

            Assert.IsTrue(environment.HasFullCredentials);
        }

        /// <summary>
        ///A test for Validate
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(ValidationException), "Non trusted authentication requires a UserName.")]
        public void ValidateEnvironmentNotTrustedWithNoUserNameTest()
        {
            SQLEnvironment environment = new SQLEnvironment();
            environment.Name = "Test";
            environment.ServerName = "Server Name";
            environment.DatabaseName = "DBName";
            environment.Trusted = false;
            environment.Validate();
        }

        /// <summary>
        ///A test for Validate
        ///</summary>
        [TestMethod()]
        public void ValidateEnvironmentNotTrustedWithNoPasswordTest()
        {
            SQLEnvironment environment = new SQLEnvironment();
            environment.Name = "Test";
            environment.ServerName = "Server Name";
            environment.DatabaseName = "DBName";
            environment.Trusted = false;
            environment.Username = "UserName";
            environment.Validate();
        }

        /// <summary>
        ///A test for Validate where the password is blank (i.e. String.Empty), not sure if people still do this bad practice.
        ///</summary>
        [TestMethod()]
        public void ValidateEnvironmentNotTrustedWithBlankPasswordTest()
        {
            SQLEnvironment environment = new SQLEnvironment();
            environment.Name = "Test";
            environment.ServerName = "Server Name";
            environment.DatabaseName = "DBName";
            environment.Trusted = false;
            environment.Username = "UserName";
            environment.Password = string.Empty;
            environment.Validate();

            Assert.IsTrue(environment.HasFullCredentials);
        }

        /// <summary>
        ///A test to ensure the Trusted and Authentication properties stay in sync.
        ///</summary>
        [TestMethod()]
        public void CheckTrustedVsAuthenticationTest()
        {
            SQLEnvironment environment = new SQLEnvironment();
            
            environment.Trusted = true;
            Assert.AreEqual(AuthenticationType.WindowsAuthentication, environment.Authentication);

            environment.Trusted = false;
            Assert.AreEqual(AuthenticationType.SQLServerAuthentication, environment.Authentication);

            // Now check that assigning to Authentication changes Trusted

            environment.Authentication = AuthenticationType.WindowsAuthentication;
            Assert.AreEqual(true, environment.Trusted);

            environment.Authentication = AuthenticationType.SQLServerAuthentication;
            Assert.AreEqual(false, environment.Trusted);
        }

    }
}
